import re, sys
from BeautifulSoup import BeautifulSoup as bs

import utils

def parse_mangas(data):
    soup = bs(data)
    rows = [row for row in soup('ul')[1].li.ul if row != u"\n"]
    text = [row.a.string.encode('utf-8') for row in rows]
    urls = [row.a['href'].encode('utf-8') for row in rows]
    return text, urls

def parse_chapters(_data):
    text, urls = [], []

    _soup = bs(_data)
    _rows = [_row for _row in _soup('ul')[3] if _row != u"\n"]
    for _row in _rows:
        pages_url = _row.a['href'].encode('utf-8')
        data = core.download(pages_url)
        soup = bs(data)
        rows = [row for row in soup('ul')[4] if row != u"\n"]
        for row in rows: text.append(row.a.string.encode('utf-8'))
        for row in rows: urls.append(row.a['href'].encode('utf-8'))

    return text, urls

pattern = re.compile("var Slides = new Array(\(.+?\));", re.S)

def parse_pages(data):
    options = [str(n) for n in xrange(len(eval(pattern.findall(data)[0])))]
    urls = ["" for x in xrange(len(options))]

    return options, urls

def parse_images_url(data, page):
    pages = eval(pattern.findall(data)[0])
    return (url + pages[int(page)][2:])

name = "itaSCAN"
url = "http://itascan.info/"
mangas_url = ""
mangas_url_ext = None
